﻿// Copyright (C) 2009  Lex Li
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Lextm.M8Pack.Components.Properties;

namespace Lextm.M8Pack.Components
{
    public partial class PhoneScreen : UserControl
    {
        private Image _framework;
        private Image _background;
        private readonly OpenFileDialog _ofdIcon = new OpenFileDialog();
        
        public PhoneScreen()
        {
            InitializeComponent();
            _ofdIcon.Filter = Resources.JpegImagesFilter;
            _ofdIcon.Title = Resources.JpegImageSelection;
        }
        
        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);
            shortcut1.Invalidate();
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            using (Graphics g = e.Graphics)
            {
                OnDraw(g);
            }
        }

        public void OnDraw(Graphics graphics)
        {
            graphics.TextRenderingHint =
                System.Drawing.Text.TextRenderingHint.AntiAlias;
            graphics.InterpolationMode =
                System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            graphics.PixelOffsetMode =
                System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            graphics.SmoothingMode =
                System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            PointF p = new PointF(0F, 0F);
            if (_background != null)
            {
                graphics.DrawImage(_background, p);
            }

            if (_framework != null)
            {
                graphics.DrawImage(_framework, p);
            }
        }

        [Bindable(true)]
        public Image Framework
        {
            get { return _framework; }
            set
            {
                _framework = value;
                Invalidate();
            }
        }
        
        [Bindable(true)]
        public Image Background
        {
            get { return _background; }
            set
            {
                _background = value;
                Invalidate();
            }
        }
        
        public event EventHandler<ImageBrowsedEventArgs> ShortcutClicked;
        
        private void shortcut1_Click(object sender, EventArgs e)
        {
            if (!Visible)
            {
                return;
            }
            
            EventHandler<ImageBrowsedEventArgs> handler = ShortcutClicked;
            if (handler != null)
            {
                if (_ofdIcon.ShowDialog() == DialogResult.OK)
                {
                    handler(this, new ImageBrowsedEventArgs(_ofdIcon.FileName));
                }
            }
        }

        public void SwitchMode()
        {
            shortcut1.Visible = !shortcut1.Visible;
        }
    }
}
